/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.Student;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.dom.DOMSource;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Nodes;
import nu.xom.ParsingException;
import nux.xom.xquery.XQueryUtil;

/**
 *
 * @author aptech
 */
public class XQueryExam {
    //ArrayList<Student>

    public ArrayList<Student> queryBy(String key, String value) {
        try {
            Document doc = new Builder().build(new File("student2.xml"));
            //demo queyString:"  //studentlist/student[name='hoang']  "
            String query = "//studentlist/student[" + key + "='" + value + "']";
//            String query = "//studentlist/student/name[ends-with(., 'g')";
            Nodes nodes = XQueryUtil.xquery(doc, query);
            ArrayList<Student> listS = new ArrayList<Student>();

            for (int i = 0; i < nodes.size(); i++) {
                String s = nodes.get(i).getValue();
                String info[] = s.split("\n");
                Student st = new Student(Integer.valueOf(
                        info[1].trim()),
                        info[2].trim(),
                        info[3].trim());
                listS.add(st);
            }
            return listS;
        } catch (ParsingException ex) {
            Logger.getLogger(XQueryExam.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XQueryExam.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

    public ArrayList<Student> queryByAtt(String value) {
        try {
            Document doc = new Builder().build(new File("student2.xml"));
            //demo queyString:"  //studentlist/student[name='hoang']  "
            String query = "//studentlist/student[@code='3']";
//            String query = "//studentlist/student/name[ends-with(., 'g')";
            String p = doc.toXML();
            FileWriter fw = new FileWriter("abc.xml");
            fw.write(p, 0, p.length());
            fw.close();
            Nodes nodes = XQueryUtil.xquery(doc, query);
            ArrayList<Student> listS = new ArrayList<Student>();
            for (int i = 0; i < nodes.size(); i++) {
                String s = nodes.get(i).getChild(0).getValue();
                s = ((Element) nodes.get(i)).getAttribute("code").getValue();
                String info[] = s.split("\n");
                Student st = new Student(
                        Integer.valueOf(s),
                        info[1].trim(),
                        info[2].trim());
                listS.add(st);
            }
            return listS;
        } catch (ParsingException ex) {
            Logger.getLogger(XQueryExam.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XQueryExam.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

    public static void main(String[] args) {
        XQueryExam xq = new XQueryExam();
//        xq.queryBy("name", "newname34");
        xq.queryBy("code", "3");
        xq.queryByAtt("3");
    }
}
